# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=installer
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.4.0
pkgrel=5
pkgdesc="A low-level library for installing from a Python wheel distribution (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/pradyunsg/installer'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-flit"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "git"
)
_commit="32836e96e14d85db1f864ab5ff46ceb84b4a7673"
_launcher_commit="8c455825fc1308d34a1a47238a9eb6d56f732142"
source=("${_realname}-$pkgver"::"git+https://github.com/pradyunsg/installer.git#commit=${_commit}"
        "simple_launcher"::"git+https://bitbucket.org/vinay.sajip/simple_launcher.git#commit=${_launcher_commit}"
        "103.patch"
        "001-launcher-relative.patch"
        "002-launcher-stderr-missing.patch")
sha256sums=('SKIP'
            'SKIP'
            '861b4ee753b7f3e1cae37e1fa9a96fe65050a7066b6650a153df5d26f10f07c0'
            '6dca4eaba0564436775ae3ddcef30152445090f16e995ca8a7b9433364ac97c7'
            'e95b68c14bffddd6dd859bdb2c8feb733bbcd5c9d4a37105df4196bfa01923c5')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/pradyunsg/installer/pull/103
  patch -Np1 -i "${srcdir}/103.patch"

  patch -Np1 -i "${srcdir}/001-launcher-relative.patch"

  cd "${srcdir}/simple_launcher"

  # https://github.com/pypa/pip/issues/10444#issuecomment-1031092176
  patch -Np1 -i "${srcdir}/002-launcher-stderr-missing.patch"
}

build() {
  cd "${srcdir}/simple_launcher"
  windres --input launcher.rc --output resources.res --output-format=coff

  local _gui_cflags="$CFLAGS $CPPFLAGS -mwindows"
  local _cli_cflags="$CFLAGS $CPPFLAGS -D_CONSOLE"
  local _gui_lflags="$LDFLAGS -lshlwapi"
  local _cli_lflags="$LDFLAGS -lshlwapi"
  case "$MSYSTEM" in
  MINGW*)
    _gui_cflags+=" -mcrtdll=msvcr120"
    _cli_cflags+=" -mcrtdll=msvcr120"
    ;;
  esac

  case "${MINGW_CHOST}" in
  i686-w64-mingw32)
    cc $_cli_cflags -o t32.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w32.exe launcher.c resources.res $_gui_lflags
    ;;
  x86_64-w64-mingw32)
    cc $_cli_cflags -o t64.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64.exe launcher.c resources.res $_gui_lflags
    ;;
  aarch64-w64-mingw32)
    cc $_cli_cflags -o t64-arm.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64-arm.exe launcher.c resources.res $_gui_lflags
    ;;
  *)
    echo "Unsupported CHOST ${MINGW_CHOST}" && false
    ;;
  esac

  cd "${srcdir}/${_realname}-${pkgver}"

  rm src/installer/_scripts/*.exe
  cp "${srcdir}/simple_launcher/"*.exe src/installer/_scripts/
  git add src/installer/_scripts/

  python -m flit build --no-setup-py
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  PYTHONPATH="${srcdir}/${_realname}-${pkgver}/src" \
    python -m installer --prefix=${MINGW_PREFIX} --destdir="${pkgdir}" dist/*.whl
}
